<?php

/**
 * @author aisyah,freaksmj
 * @copyright 2012
 */
 
include "store.php";

class Store_Dao{
    
    var $store = array();
    var $store_id;
    var $query;
    var $error;
    var $db;
    
    public function __construct(Database $db){
        $this->db = $db;
    }    
    
    public function add_store(Store $store){
		
		$this->db->connect->connect();
		
		if($this->db->table_Exists('store')){
			
			$this->query = "
							INSERT
							INTO
							store
							(
								store_name,
								description,
								street,
								kecamatan,
								city,
								province,
								zip_code,
								logo,
								image
							)
							VALUES
							(
								'".$store->store_name."',
								'".$store->description."',
								'".$store->street."',
								'".$store->kecamatan."',
								'".$store->city."',
								'".$store->province."',
								'".$store->zip_code."',
								'".$store->logo."',
								'".$store->image."'
							)
							";
			mysql_query($this->query);
            $this->db->connect->disconnect();			
			
		}else{
            
            $this->error = 'data tidak ditemukan';
            return $this->error;
            
        }          
		
	}
	
	public function get_store($id=null, $posisi=null, $batas=null){
        
        $this->db->connect->connect();
        
        if($this->db->table_Exists('store')){
            
            if($id!=null AND $posisi==null AND $batas==null){
                $this->query = "SELECT
                        *
                        FROM
                        store
                        WHERE 
                        store_id = '".$id."'
            
                    ";
            }else if($id!=null AND !is_null($posisi) AND !is_null($batas==null)){
                $this->query = "SELECT
                        *
                        FROM
                        store
                        WHERE 
                        store_id = '".$id."'
            
                        LIMIT '".$posisi."','".$batas;
            }else if(!is_null($posisi) AND !is_null($batas==null)){
                $this->query = "SELECT
                        *
                        FROM
                        store               
                        
            
                        LIMIT $posisi,$batas";
            }else{
                $this->query = "SELECT
                        *
                        FROM
                        store      
                    ";
            }
            
            //print_r($this->query);
            $result = mysql_query($this->query);  
            
            if($result){
                
                while($r=mysql_fetch_array($result)){
                    
                    $store = new Store();
                    
                    $store->store_id = $r['store_id'];
                    $store->store_name = $r['store_name'];
                    $store->description = $r['description'];
                    $store->street = $r['street'];
                    $store->kecamatan = $r['kecamatan'];
                    $store->city = $r['city'];
                    $store->province = $r['province'];
                    $store->zip_code = $r['zip_code'];
                    $store->logo = $r['logo'];
                    $store->image = $r['image'];
                    
                    $this->store[] = $store;
                    
                    
                }
                
                $this->db->connect->disconnect();
            
                return $this->store;
            }else{
                
                $this->error = 'data tidak ditemukan';
                return $this->error;
                
            }      
            
        }else{
            
            $this->error = 'data tidak ditemukan';
            return $this->error;
            
        }             
        
    }
    
    public function get_store_name($username){
        
        $q="SELECT user.user_id, store.store_name as name FROM user INNER JOIN store
            ON user.user_id=store.user_id WHERE user.username='".$username."'";
        
            
        //print_r($q);
        
        $hasil = $this->db->select_table($q);
        print_r($hasil);
        foreach($hasil as $data){
            $nama_toko = $data['name'];
        }
        
        return $nama_toko;
    } 
    
    
}